﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using StockTrader.Common;

namespace StockTrader.Platform.Charting
{
    public partial class StockChart : UserControl
    {
        public StockChart()
        {
            InitializeComponent();

            SetupChart();
        }


        public void SetupChart()
        {
            //// Predefine the viewing area of the chart
            //minValue = DateTime.Now;
            //maxValue = minValue.AddSeconds(120);
            //chart1 = new Chart();

            chart1.ChartAreas[0].AxisX.Minimum = DateTime.Now.AddYears(-7).ToOADate();
            chart1.ChartAreas[0].AxisX.Maximum = DateTime.Now.ToOADate();


            // Reset number of series in the chart.
            chart1.Series.Clear();

            // create a line chart series
            Series newSeries = new Series("Series1")
                                   {
                                       ChartType = SeriesChartType.Line,
                                       BorderWidth = 2,
                                       Color = Color.OrangeRed,
                                       XValueType = ChartValueType.DateTime,
                                       YValueType = ChartValueType.Double
                                   };
            chart1.Series.Add(newSeries);

            // create a delegate for adding data
            addDataDel += new AddTickChartDelegate(AddTick);
            
        }
        private void StockChart_Load(object sender, EventArgs e)
        {
            // create a delegate for adding data
            //addDataDel += new AddTickChartDelegate(AddTick);
        }


        // Thread Add Data delegate
        public AddTickChartDelegate addDataDel;

        public void AddTick(ChartTickPoint point)
        {
            DateTime timeStamp = point.X;
            double price = point.Y;

            foreach (Series ptSeries in chart1.Series)
            {
                AddNewPoint(timeStamp, price, ptSeries);
            }
        }

        /// The AddNewPoint function is called for each series in the chart when
        /// new points need to be added.  The new point will be placed at specified
        /// X axis (Date/Time) position with a Y value in a range +/- 1 from the previous
        /// data point's Y value, and not smaller than zero.
        public void AddNewPoint(DateTime timeStamp, double price, Series ptSeries)
        {
            //double newVal = 0;

            //if (ptSeries.Points.Count > 0)
            //{
            //    newVal = ptSeries.Points[ptSeries.Points.Count - 1].YValues[0] + ((rand.NextDouble() * 2) - 1);
            //}

            //if (newVal < 0)
            //    newVal = 0;

            // Add new data point to its series.
            ptSeries.Points.AddXY(timeStamp.ToOADate(), price);

            // remove all points from the source series older than 1.5 minutes.
            //double removeBefore = timeStamp.AddSeconds((double)(90) * (-1)).ToOADate();
            ////remove oldest values to maintain a constant number of data points
            //while (ptSeries.Points[0].XValue < removeBefore)
            //{
            //    ptSeries.Points.RemoveAt(0);
            //}

            //chart1.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue;
            //chart1.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddMinutes(2).ToOADate();

            chart1.Invalidate();
        }

        /// Clean up any resources being used.
        protected override void Dispose(bool disposing)
        {
            //if ((addDataRunner.ThreadState & ThreadState.Suspended) == ThreadState.Suspended)
            //{
            //    addDataRunner.Resume();
            //}
            //addDataRunner.Abort();

            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
    }
}
